package com.luoran.bartalks.action;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.entity.biz.BtMateriel;
import com.luoran.bartalks.entity.biz.BtMaterielType;
import com.luoran.bartalks.service.IBtMaterielService;
import com.luoran.bartalks.service.IBtMaterielTypeService;

@Controller
@RequestMapping("/materielType")
public class BtMaterielTypeAction {

	@Autowired
	private IBtMaterielTypeService materielTypeService;

	@Autowired
	private IBtMaterielService materielService;

	private static final Logger log = LoggerFactory.getLogger(BtPositionAction.class);

	// 获取物料类型
	@GetMapping("/getMaterielType")
	@ResponseBody
	public JSONObject materielType() {
		log.info("获取物料类型信息");
		JSONObject res = new JSONObject();
		List<BtMaterielType> materielTypes = materielTypeService
				.getMaterielTypeByCompanyId(UserContext.get().getCompanyId());
		res.put("obj", materielTypes);
		res.put("state", true);
		return res;
	}

	// 新增物料类型
	@PostMapping("/addMaterielType")
	@ResponseBody
	public JSONObject addMaterielType(BtMaterielType btMaterielType) {
		log.info("新增物料类型信息>>");
		JSONObject res = new JSONObject();

		String regex = "^[0-9a-zA-Z]{1,20}$";
		if (!btMaterielType.getTypeCode().matches(regex)) {
			res.put("msg", "物料类型编码不合法");
			res.put("state", false);
			return res;
		}

		if (StringUtils.isEmpty(btMaterielType.getTypeName())) {
			res.put("msg", "物料类型名称不可以为空");
			res.put("state", false);
			return res;
		}

		// 物料类型编码重复校验
		log.info("校验物料类型编码[{}]是否被占用>>>", btMaterielType.getTypeCode());
		BtMaterielType materielType = materielTypeService.getMaterielTypeByTypeCode(btMaterielType.getTypeCode(),
				UserContext.get().getCompanyId());
		if (materielType == null) {// 未使用的编码
			btMaterielType.setCompanyId(UserContext.get().getCompanyId());
			btMaterielType.setCreateTime(new Date());
			btMaterielType.setStatus(0);// 默认状态
			// 插入数据库并返回
			BtMaterielType btMaterie = materielTypeService.addMaterielType(btMaterielType);
			res.put("obj", btMaterie);
			res.put("msg", "新增成功");
			res.put("state", true);
			return res;
		} else {
			log.info("物料类型编码[{}]被占用<<<<", btMaterielType.getTypeCode());
			res.put("msg", "物料类型编码“" + btMaterielType.getTypeCode() + "”已被使用");
			res.put("state", false);
			return res;
		}
	}

	// 编辑物料类型
	@PostMapping("/editMaterielType")
	@ResponseBody
	public JSONObject editMaterielType(BtMaterielType btMaterielType) {
		log.info("编辑物料类型信息>>");
		JSONObject res = new JSONObject();

		if (StringUtils.isEmpty(btMaterielType.getTypeName())) {
			res.put("msg", "物料类型名称不可以为空");
			res.put("state", false);
			return res;
		} else if (btMaterielType.getTypeName().length() > 100) {
			res.put("msg", "物料类型名称长度限制为100");
			res.put("state", false);
			return res;
		}
		int count = materielTypeService.updateMaterielTypeById(btMaterielType);
		if (count != 1) {
			res.put("msg", "操作失败");
			res.put("state", false);
			return res;
		}
		res.put("msg", "操作成功");
		res.put("state", true);
		return res;
	}

	// 逻辑删除物料类型
	@PostMapping("/deleteMaterielType/{id}")
	@ResponseBody
	public JSONObject deleteMaterielType(@PathVariable("id") String id) {
		log.info("删除物料类型>>>");
		BtMaterielType materielType = new BtMaterielType();
		materielType.setId(id);
		materielType.setStatus(1);
		JSONObject res = new JSONObject();
		int count = materielTypeService.updateMaterielTypeById(materielType);
		if (count != 1) {
			res.put("msg", "操作失败");
			res.put("state", false);
			return res;
		}
		res.put("msg", "操作成功");
		res.put("state", true);
		return res;
	}

	// 通过物料类型ID校验类型是否被引用
	@RequestMapping("/hasMateriel/{id}")
	@ResponseBody
	public JSONObject checkMaterielType(@PathVariable("id") String typeId) {
		log.info("校验该物料类型是否被引用>>>>>");
		JSONObject result = new JSONObject();
		List<BtMateriel> materiels = materielService.getMaterielByTypeId(UserContext.get().getCompanyId(), typeId);
		if (materiels.size() != 0) {
			log.info("该物料类型已被引用<<<<<");
			result.put("msg", "当前物料类型已被引用,无法操作");
			result.put("flag", true);
			return result;
		} else {
			log.info("该物料类型未被引用<<<<<");
			result.put("flag", false);
			return result;
		}

	}

}
